//封装echarts
class Echarts {
  /**
   *
   * @param {str} type 图标的类型
   * @param {dom} dom
   */
  constructor(type, dom) {
    this.baseOptions = {
      title: {}
    };
    this.type = type;
    this.dom = dom;
    this.echart = this.init();
  }
  //获取echarts实例
  init() {
    return echarts.init(this.dom);
  }
  //设置echart参数
  setOption(option) {
    this.option = option;
    var dataOption = {};
    var otherOption = this.option.otherOption || {};
    switch (this.type) {
      case "bar":
        dataOption = this.setBarOption();
        break;
      case "pie":
        dataOption = this.setPieOption();
        break;
    }
    Object.assign(this.baseOptions, dataOption, otherOption);
    this.echart.setOption(this.baseOptions);
  }
  // 设置折线图;
  setBarOption() {
    var data = this.option || {};
    var name = data.name || "";
    var list = data.data || [];
    var labels = list.map((item) => {
      return item.name;
    });
    var values = list.map((item) => {
      return item.value;
    });
    var dataOption = {
      legend: {
        data: [name]
      },
      tooltip: {},
      xAxis: {
        data: labels
      },
      yAxis: {},
      series: [
        {
          name: name,
          type: "bar",
          data: values
        }
      ]
    };
    return dataOption;
  }
  setPieOption() {
    var data = this.option || {};
    var name = data.name || "";
    var list = data.data || [];
    var labels = list.map((item) => {
      return item.name;
    });
    var dataOption = {
      legend: {
        data: labels,
        orient: "vertical",
        left: "left"
      },
      tooltip: {
        trigger: "item",
        formatter: "{a} <br/>{b} : {c} ({d}%)"
      },

      series: [
        {
          name: name,
          type: "pie",
          data: list,
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: "rgba(0, 0, 0, 0.5)"
            }
          }
        }
      ]
    };
    return dataOption;
  }
  // 设置
  //返回echart实例
  getEcharts() {
    return this.echart;
  }
}
